package com.mls.fivedistrict.server.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.dao.NewsCommentMapper;
import com.mls.fivedistrict.pojo.NewsComment;
import com.mls.fivedistrict.pojo.UserLikeComment;
import com.mls.fivedistrict.server.NewsCommentService;
import com.mls.fivedistrict.server.UserLikeCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 新闻评论 服务实现类
 * </p>
 *
 * @author Five Team
 * @since 2018-05-15
 */
@Service
public class NewsCommentServiceImpl extends ServiceImpl<NewsCommentMapper, NewsComment> implements NewsCommentService {
    @Autowired
    NewsCommentMapper newsCommentMapper;

    @Override
    public boolean addLikeTimes(long id) {
        return newsCommentMapper.addLikeTimes(id);
    }

    @Override
    public boolean cutLikeTimes(long id) {
        return newsCommentMapper.cutLikeTime(id);
    }

    @Override
    public boolean addReplyTimes(long id) {
        return newsCommentMapper.addReplyTimes(id);
    }

    @Override
    public boolean cutReplyTimes(long id) {
        return newsCommentMapper.cutReplyTimes(id);
    }

    @Autowired
    UserLikeCommentService userLikeCommentService;

    @Override
    public List<NewsComment> getNewsComment(Long newsId, PageFilter pageFilter) {
        Wrapper wrapper = pageFilter.getWrapper();
        if (wrapper == null) {
            wrapper = Condition.create();
        }
        wrapper.eq("parent_comment_id", 0);
        wrapper.eq("news_id", newsId);
        wrapper.orderBy("create_date", false);
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<NewsComment> selectList = selectList(wrapper);

        if (RequestHolder.getCurrentUser() != null) {
            for (NewsComment newsComment : selectList) {
                Wrapper userNewsCommentWrapper = Condition.create();
                userNewsCommentWrapper.eq("user_id", RequestHolder.getCurrentUser().getId());
                userNewsCommentWrapper.eq("comment_id", newsComment.getId());
                UserLikeComment userLikeComment = userLikeCommentService.selectOne(userNewsCommentWrapper);
                if (userLikeComment != null) {
                    newsComment.setLike(true);
                }
            }
        }
        for (NewsComment newsComment : selectList) {
            disposeNewsComment(newsComment);
        }
        return selectList;
    }

    private void disposeNewsComment(NewsComment newsComment) {
        List<NewsComment> childrenNewsComment = newsComment.getChildrenNewsComment();
        List<NewsComment> copyNewsComment = new ArrayList();
        for (NewsComment comment : childrenNewsComment) {
            copyNewsComment.add(comment);
        }
        for (NewsComment comment : copyNewsComment) {
            newsComment.getChildrenNewsComment().addAll(comment.getChildrenNewsComment());
            for (NewsComment ch : comment.getChildrenNewsComment()) {
                disposeNextComment(newsComment, ch);
            }
            comment.setChildrenNewsComment(null);

        }

    }

    private void disposeNextComment(NewsComment newsComment, NewsComment childComment) {
        List<NewsComment> childrenNewsComment = childComment.getChildrenNewsComment();
        newsComment.getChildrenNewsComment().addAll(childrenNewsComment);
        for (NewsComment comment : childrenNewsComment) {
            disposeNextComment(newsComment, comment);
        }
        childComment.setChildrenNewsComment(null);
    }
}
